/*var data={imgurl:'http://img-qcloud.hucai.com/2017/08/02/201708021033511190.jpg',canvasw:351,canvash:386};
var canvas_img=document.getElementById("reallycanvas");
*/	
//生成的图片 移动裁剪


//移动图片
var canvas_img=document.getElementById("reallycanvas");	
var ctx_2d = canvas_img.getContext("2d");
function canvasdrag(){
	var currentid=canvas_img.getAttribute("currentid");
		ctxmsg[currentid].imgoldw = ctxmsg[currentid].img.width,
		ctxmsg[currentid].imgoldh = ctxmsg[currentid].img.height;
		if(ctxmsg[currentid].transform>0){
			canvasimgrote()
		}else{
			drawNewImg();
		}
		var touchs = {
		startx: 0,
		starty: 0,
		endx: 0,
		endy: 0,
		startx2: 0,
		starty2: 0,
		endx2: 0,
		endy2: 0,
	};
	canvas_img.addEventListener("touchstart", canvasTouchStart, false);
	canvas_img.addEventListener("touchmove", canvasTouchMove, false);
	canvas_img.addEventListener("touchend", canvasTouchEnd, false);

	function canvasTouchEnd(e) {

	}

	function canvasTouchStart(e) {
		var currentid=canvas_img.getAttribute("currentid");
		e.preventDefault();
		touchs.startx = e.touches[0].pageX*Pixel_Ratio;
		touchs.starty = e.touches[0].pageY*Pixel_Ratio;
		if(e.targetTouches.length == 1) {
			//console.log("canvasTouchStart 单手触摸")
			ctxmsg[currentid].imgoldx = ctxmsg[currentid].imgx;
			ctxmsg[currentid].imgoldy = ctxmsg[currentid].imgy;
		} else if(e.targetTouches.length == 2) {
			//console.log("canvasTouchStart 单手触摸")
			/*touchs.startx2 = e.touches[1].pageX;
			touchs.starty2 = e.touches[1].pageY;
			ctxmsg[currentid].imgoldw = ctxmsg[currentid].imgw;
			ctxmsg[currentid].imgoldh = ctxmsg[currentid].imgh;*/
			return;
		}
	}

	function canvasTouchMove(e) {
		var currentid=canvas_img.getAttribute("currentid");
		ctxmsg[currentid].ischange=true;
		e.preventDefault();
		touchs.endx = e.touches[0].pageX*Pixel_Ratio;
		touchs.endy = e.touches[0].pageY*Pixel_Ratio;
		if(e.touches.length == 1) {
			console.log("canvasTouchMove 单手触摸")
			ctxmsg[currentid].imgx = ctxmsg[currentid].imgoldx + touchs.endx - touchs.startx;
			ctxmsg[currentid].imgy = ctxmsg[currentid].imgoldy + touchs.endy - touchs.starty;
			//固定区域内移动，画图区域不留白    
			if(ctxmsg[currentid].imgw <= ctxmsg[currentid].w || ctxmsg[currentid].imgx > ctxmsg[currentid].x) {
				ctxmsg[currentid].imgx = ctxmsg[currentid].x;
			} else if(ctxmsg[currentid].imgw > ctxmsg[currentid].w && ctxmsg[currentid].imgx <= ctxmsg[currentid].x - (ctxmsg[currentid].imgw - ctxmsg[currentid].w)) {
				ctxmsg[currentid].imgx = ctxmsg[currentid].x - (ctxmsg[currentid].imgw - ctxmsg[currentid].w);
			}
			if(ctxmsg[currentid].imgh <= ctxmsg[currentid].h || ctxmsg[currentid].imgy > ctxmsg[currentid].y) {
				ctxmsg[currentid].imgy = ctxmsg[currentid].y;
			} else if(ctxmsg[currentid].imgh > ctxmsg[currentid].h && ctxmsg[currentid].imgy <= ctxmsg[currentid].y - (ctxmsg[currentid].imgh - ctxmsg[currentid].h)) {
				ctxmsg[currentid].imgy = ctxmsg[currentid].y - (ctxmsg[currentid].imgh - ctxmsg[currentid].h);
			}
		
		} else if(e.touches.length == 2) {
			console.log("canvasTouchMove 双手触摸 11")
			/*touchs.endx2 = e.touches[1].pageX;
			touchs.endy2 = e.touches[1].pageY;
			
			//var xdiff = x2 - x1;    
			//var ydiff = y2 - y1;    
			var xdiff1 = touchs.startx2 - touchs.startx;
			var ydiff1 = touchs.starty2 - touchs.starty;
			var line1 = Math.sqrt(Math.pow(xdiff1, 2) + Math.pow(ydiff1, 2));

			var xdiff2 = touchs.endx2 - touchs.endx;
			var ydiff2 = touchs.endy2 - touchs.endy;
			var line2 = Math.sqrt(Math.pow(xdiff2, 2) + Math.pow(ydiff2, 2));

			img_new_w = ctxmsg[currentid].imgw + (line2 - line1) / 20;
			img_new_h = img_new_w / ctxmsg[currentid].imgw * ctxmsg[currentid].imgh;
			//固定区域内缩放，画图区域不留白    
			if(img_new_w <= ctxmsg[currentid].w || img_new_h <= ctxmsg[currentid].h) {
				return;
			}

			ctxmsg[currentid].imgx -= img_new_w - ctxmsg[currentid].imgw;
			ctxmsg[currentid].imgy -= img_new_h - ctxmsg[currentid].imgh;

			ctxmsg[currentid].imgw = img_new_w;
			ctxmsg[currentid].imgh = img_new_h;*/
			return;
		}
		//固定区域内移动，画图区域不留白    
		/*if(ctxmsg[currentid].imgw <= ctxmsg[currentid].w || ctxmsg[currentid].imgx > ctxmsg[currentid].x) {
			ctxmsg[currentid].imgx = ctxmsg[currentid].x;
		} else if(ctxmsg[currentid].imgw > ctxmsg[currentid].w && ctxmsg[currentid].imgx <= ctxmsg[currentid].x - (ctxmsg[currentid].imgw - ctxmsg[currentid].w)) {
			ctxmsg[currentid].imgx = ctxmsg[currentid].x - (ctxmsg[currentid].imgw - ctxmsg[currentid].w);
		}
		if(ctxmsg[currentid].imgh <= ctxmsg[currentid].h || ctxmsg[currentid].imgy > ctxmsg[currentid].y) {
			ctxmsg[currentid].imgy = ctxmsg[currentid].y;
		} else if(ctxmsg[currentid].imgh > ctxmsg[currentid].h && ctxmsg[currentid].imgy <= ctxmsg[currentid].y - (ctxmsg[currentid].imgh - ctxmsg[currentid].h)) {
			ctxmsg[currentid].imgy = ctxmsg[currentid].y - (ctxmsg[currentid].imgh - ctxmsg[currentid].h);
		}*/
		drawNewImg();
	}

	
	
}

function drawNewImg() {
		var currentid=canvas_img.getAttribute("currentid");
		ctx_2d.clearRect(ctxmsg[currentid].x, ctxmsg[currentid].y, ctxmsg[currentid].w, ctxmsg[currentid].h);
		ctx_2d.rect(ctxmsg[currentid].x, ctxmsg[currentid].y, ctxmsg[currentid].w, ctxmsg[currentid].h);
		ctx_2d.save();
		ctx_2d.clip();		
		if(!ctxmsg[currentid].imgw) {
			//设置画入画布内的图片宽、高（等比例铺满指定盒子，溢出被剪切掉）    
			if(ctxmsg[currentid].imgoldw / ctxmsg[currentid].imgoldh > ctxmsg[currentid].w / ctxmsg[currentid].h) {
				ctxmsg[currentid].imgh = ctxmsg[currentid].h;
				ctxmsg[currentid].imgw = ctxmsg[currentid].h / ctxmsg[currentid].imgoldh * ctxmsg[currentid].imgoldw;
			} else {
				ctxmsg[currentid].imgw = ctxmsg[currentid].w;
				ctxmsg[currentid].imgh = ctxmsg[currentid].w / ctxmsg[currentid].imgoldw * ctxmsg[currentid].imgoldh;
			}
		}		
		if(ctxmsg[currentid].transform>0) {
			
			ctx_2d.drawImage(create_cvs, ctxmsg[currentid].imgx, ctxmsg[currentid].imgy, ctxmsg[currentid].imgw, ctxmsg[currentid].imgh);
		/*	alert(ctxmsg[currentid].w +'   '+ctxmsg[currentid].h)
			alert('x:'+ctxmsg[currentid].imgx)
			alert('x:'+ctxmsg[currentid].imgy)
			alert('x:'+ctxmsg[currentid].imgw)			
			alert('x:'+ctxmsg[currentid].imgh)*/
		} else {
			ctx_2d.drawImage(ctxmsg[currentid].img, ctxmsg[currentid].imgx, ctxmsg[currentid].imgy, ctxmsg[currentid].imgw, ctxmsg[currentid].imgh);
		}
		ctx_2d.restore();
	}

	//旋转canvas中的图片    
	var create_cvs = document.createElement('canvas');
	var create_cvs_2d = create_cvs.getContext('2d');	
	//document.getElementById("roat_btn").addEventListener("click", canvasimgrote, false);	
	function canvasimgrote(){
		var currentid=canvas_img.getAttribute("currentid");	
		ctxmsg[currentid].transform = ctxmsg[currentid].transform % 360;
	
		var w = ctxmsg[currentid].img.width;
		var h = ctxmsg[currentid].img.height;
	
		var x = w / -2;
		var y = h / -2;
	
		if(ctxmsg[currentid].transform % 180 != 0) {
			create_cvs.width = h;
			create_cvs.height = w;
			if(w > h) {
				//console.log(1);
				ctxmsg[currentid].imgw = ctxmsg[currentid].w;
				ctxmsg[currentid].imgh = ctxmsg[currentid].w / h * w;
			} else {
				//console.log(2);
				ctxmsg[currentid].imgh = ctxmsg[currentid].h;
				ctxmsg[currentid].imgw = ctxmsg[currentid].h / w * h;
			}
		} else {
			create_cvs.width = w;
			create_cvs.height = h;
			if(w > h) {
				//console.log(3);
				ctxmsg[currentid].imgh = ctxmsg[currentid].h;
				ctxmsg[currentid].imgw = ctxmsg[currentid].h / h * w;
			} else {
				//console.log(4);
				ctxmsg[currentid].imgw = ctxmsg[currentid].w;
				ctxmsg[currentid].imgh = ctxmsg[currentid].w / w * h;
			}
		}
	 	create_cvs_2d.clearRect(0, 0, create_cvs.width, create_cvs.height);
		create_cvs_2d.translate(create_cvs.width / 2, create_cvs.height / 2);
		create_cvs_2d.rotate(ctxmsg[currentid].transform * Math.PI / 180);
		create_cvs_2d.drawImage(ctxmsg[currentid].img, x, y);
	
		//防止旋转图片后，指定画图区留白（图片小于画图区）  
		if(ctxmsg[currentid].imgw < ctxmsg[currentid].w) {
			ctxmsg[currentid].imgh = ctxmsg[currentid].w / ctxmsg[currentid].imgw * ctxmsg[currentid].imgh;
			ctxmsg[currentid].imgw = ctxmsg[currentid].w;
		}
		if(ctxmsg[currentid].imgh < ctxmsg[currentid].h) {
			ctxmsg[currentid].imgw = ctxmsg[currentid].h / ctxmsg[currentid].imgh * ctxmsg[currentid].imgw;
			ctxmsg[currentid].imgh = ctxmsg[currentid].h;
		}
	
		ctxmsg[currentid].imgx = ctxmsg[currentid].x;
		ctxmsg[currentid].imgy = ctxmsg[currentid].y;
		//固定区域内移动，画图区域不留白    
		if(ctxmsg[currentid].imgw <= ctxmsg[currentid].w || ctxmsg[currentid].imgx > ctxmsg[currentid].x) {
				ctxmsg[currentid].imgx = ctxmsg[currentid].x;
			} else if(ctxmsg[currentid].imgw > ctxmsg[currentid].w && ctxmsg[currentid].imgx <= ctxmsg[currentid].x - (ctxmsg[currentid].imgw - ctxmsg[currentid].w)) {
				ctxmsg[currentid].imgx = ctxmsg[currentid].x - (ctxmsg[currentid].imgw - ctxmsg[currentid].w);
			}
			if(ctxmsg[currentid].imgh <= ctxmsg[currentid].h || ctxmsg[currentid].imgy > ctxmsg[currentid].y) {
				ctxmsg[currentid].imgy = ctxmsg[currentid].y;
			} else if(ctxmsg[currentid].imgh > ctxmsg[currentid].h && ctxmsg[currentid].imgy <= ctxmsg[currentid].y - (ctxmsg[currentid].imgh - ctxmsg[currentid].h)) {
				ctxmsg[currentid].imgy = ctxmsg[currentid].y - (ctxmsg[currentid].imgh - ctxmsg[currentid].h);
		}
		
		drawNewImg();
		
	};

function singerupload(){
	$(".up_page").show();
	
	
	
}

//替换图片
function canvasimgreplace(imgurl,image_id,width,height){// lym 2018.01.02 修改 图片加载 原先的的id标志转为url标志
	//imgurl=imgurl.split('?')[0]+'imageMogr2/thumbnail/';
	if(UtilProperty[work_id].imageObj[imgurl]==undefined){			
		var mulitImg=new Array;	
		mulitImg.push(imgurl);		
		imagelistToObject(mulitImg,work_id,function(){
		//替换图片 更改ctxmsg 和当前的canvas_img
		changereplaceimg(image_id,imgurl,width,height);
		})
	}else{
		
		changereplaceimg(image_id,imgurl,width,height);
		
	}
}
//替换图片 更改ctxmsg 和当前的canvas_img
function changereplaceimg(image_id,imgurl,width,height){
	var pageno = $("#originalcanvas").attr("page");
	var canvasData = UtilProperty[work_id].worker[pageno].control_list;
	var currentid = canvas_img.getAttribute("currentid");
	for(var i = 0; i < canvasData.length; i++) {
		var item = canvasData[i];
		if(item.id == currentid) {
			var scale = ctxmsg[currentid].scale;
			ctxmsg[item.id] = {
				imgurl: imgurl,
				img: UtilProperty[work_id].imageObj[image_id],
				image_id: image_id,
				x: 0,
				y: 0,
				w: item.width * scale,
				h: item.height * scale,
				imgx: item.element_list[0].cut_x * scale,
				imgy: item.element_list[0].cut_y * scale,
				imgw: 0,
				imgh: 0,
				imgoldx: 0,
				imgoldy: 0,
				imgoldw: 0,
				imgoldh: 0,
				imgwid:width,
				imghei:height,
				transform: 0,
				scale: scale,
				ischange: true
			};
			canvas_img.width = ctxmsg[currentid].w;
			canvas_img.height = ctxmsg[currentid].h;
			//固定区域内移动，画图区域不留白    
			if(ctxmsg[currentid].imgw <= ctxmsg[currentid].w || ctxmsg[currentid].imgx > ctxmsg[currentid].x) {
				ctxmsg[currentid].imgx = ctxmsg[currentid].x;
			} else if(ctxmsg[currentid].imgw > ctxmsg[currentid].w && ctxmsg[currentid].imgx <= ctxmsg[currentid].x - (ctxmsg[currentid].imgw - ctxmsg[currentid].w)) {
				ctxmsg[currentid].imgx = ctxmsg[currentid].x - (ctxmsg[currentid].imgw - ctxmsg[currentid].w);
			}
			if(ctxmsg[currentid].imgh <= ctxmsg[currentid].h || ctxmsg[currentid].imgy > ctxmsg[currentid].y) {
				ctxmsg[currentid].imgy = ctxmsg[currentid].y;
			} else if(ctxmsg[currentid].imgh > ctxmsg[currentid].h && ctxmsg[currentid].imgy <= ctxmsg[currentid].y - (ctxmsg[currentid].imgh - ctxmsg[currentid].h)) {
				ctxmsg[currentid].imgy = ctxmsg[currentid].y - (ctxmsg[currentid].imgh - ctxmsg[currentid].h);
			}
			canvasdrag();			
			$('.editoritem[currentid="'+currentid+'"]').css("background-image",'url('+UtilProperty[work_id].imageObj[imgurl].src+')');// lym 2018.01.04 修改 图片加载 原先的的id标志转为url标志
			
			if($("#loadimground").is(":hidden")){
				$("#loadimground").hide();
				$("#load p").html('loading');
			}	
			
			if(!$("#sing_up_page").is(":hidden")){			
			  $.closePopup("#sing_up_page");
			}
			break;
		}
	}
	
}
